<?php
abstract class service_module extends module {
	protected $_callbacks = array();
	public $_extended = array();
	protected $_my_server;
	final public function __construct(server &$my_server) {
		$this->name = str_replace('service_','',get_class($this));
		$this->_my_server = $my_server;
		if (is_callable(array(&$this,'_run')))
			if ($this->_run()) return;
		if ($GLOBALS['protocol']->me->status < server::STATUS_ENDOFSYNC)
			$this->_callbacks[] = new callback_on_eos(array(&$this,'on_eos'),array(true));
		else
			$this->on_eos(false);
	}
	public function destruct() {
		foreach ($this->_callbacks as $key => &$callback)
			unset($this->_callbacks[$key]);
	}
	public function privmsg_handler(basic &$source, user &$target, $message) {
		$command = strtolower(firstword($message,$parameters));
		if ($source instanceof channel) throw new Exception("Unexpected basic source",029);
		if ($source instanceof server or !$source->ngi) $access = info_nickgroup::status_none;//0;
		else $access = $source->ngi->status;
		$run = (array_key_exists($command,$this->_extended) ? $this->_extended[$command] :
		array(&$this, $command . '_'));
		if (!is_callable(array(&$this,$cmd))) {
			//TODO $hs->helpnotice($u,$this->source,'UNKNOWN_COMMAND','c',$command);
			return;
		}
		$ret = call_user_func_array($run, array(&$source, &$target, $access, $parameters));
		if (is_int($ret)) switch ($ret) {
			case 1:
				//$hs->helpnotice($u,$this->source,'ACCESS_DENIED');
				break;
			case 2:
				//$hs->helpnotice($u,$this->source,'BAD_SYNTAX','c',$command);
				//$hs->helpnotice($u,$this->source,'MORE_INFO','s',$this->name,'p',$command);
				break;
			case 3:
				//$hs->helpnotice($u,$this->source,'INTERNAL_ERROR');
				//errlog($this->name.':'.$command,'INTERNAL_ERROR');
				break;
			case 4:
				//$hs->helpnotice($u,$this->source,'MISSING_QUOTE');
				break;
			case 5:
				//$hs->helpnotice($u,$this->source,'READ_ONLY_MODE');
				break;
			case 7:
				//$hs->helpnotice($u,$this->source,'SERVICES_IS_BUSY');
				//errlog($this->name.':'.$command,'SERVICES_IS_BUSY');
				break;
		} elseif (is_string($ret)) {
			//errlog($this->name.':'.$command, "From: $source To: $target Parameters: $parameters Error: $ret");
		}

	}
	abstract protected function on_eos ($state);
	//nickserv chanserv memoserv operserv helpserv etc
}
?>